﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace GPM
{
    public partial class FrmWebpart : BaseForm
    {
        public GridCheckMarksSelection selection;
        public GridCheckMarksSelection selectionButton;
        private int CurrentWebpartID;
        public FrmWebpart()
        {
            InitializeComponent();
            this.BarComponents = components;
        }

        private void FrmWebpart_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvWebpart);
            selectionButton = new GridCheckMarksSelection(this.gvButton);
            //colPostUrl.DisplayFormat.Format = new ButtonExtFormat();
            RefreshWebpart();
        }

        public void RefreshWebpart()
        {
            WebpartController uc = new WebpartController();
            IList<CP.Entities.Webpart> Webparts = uc.GetAllWebparts();
            ModuleController mc = new ModuleController();
            this.lookupeditParentID.DataSource = mc.GetAllModules();
            //selection.ClearSelection();
            this.gcWebpart.DataSource = Webparts;
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshWebpart();
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;

            IList<int> webpartids = new List<int>();
            foreach (object o in selection.Selection)
            {
                CP.Entities.Webpart webpart = o as CP.Entities.Webpart;
                if (null != webpart)
                    webpartids.Add(webpart.webpart_id);
            }
            try
            {
                WebpartController gc = new WebpartController();
                gc.DeleteWebparts(webpartids);
                LogHelper.Log("组件管理", "delete", "删除", "Webpartids:" + string.Join(",", webpartids));
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                RefreshWebpart();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmInitWebpart frmedit = new FrmInitWebpart();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            CP.Entities.Webpart info = selection.Selection[0] as CP.Entities.Webpart;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }

            FrmInitWebpart frmedit = new FrmInitWebpart("1", info.webpart_id);
            frmedit.ShowDialog(this);
        }

        private void gvWebpart_DoubleClick(object sender, EventArgs e)
        {
            CP.Entities.Webpart webpart = gvWebpart.GetFocusedRow() as CP.Entities.Webpart;
            if (null == webpart)
                return;

            FrmInitWebpart frm = new FrmInitWebpart("1",webpart.webpart_id);
            frm.ShowDialog(this);
        }

        private void gvWebpart_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            CP.Entities.Webpart webpart = gvWebpart.GetFocusedRow() as CP.Entities.Webpart;
            if (null == webpart)
                return;
            CurrentWebpartID = webpart.webpart_id;
            BindingButton();
            BindingList();
        }

        private void BindingButton()
        {
            WebpartController wc = new WebpartController();
            gcButton.DataSource = wc.GetButtonsByWebpartID(CurrentWebpartID);
        }

        private void BindingList()
        {
            WebpartController wc = new WebpartController();
            gcList.DataSource = wc.GetListsByWebpartID(CurrentWebpartID);
        }

        private void barInitWebpart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmWebpartDraw frm = new FrmWebpartDraw();
            frm.ShowDialog();
        }

        private void gvButton_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            CP.Entities.Button_List btn = gvButton.GetFocusedRow() as CP.Entities.Button_List;
            if (null == btn)
                return;
            this.txtBtnID.Text = btn.button_id.ToString();
            this.txtBtnName.Text = btn.button_name;
            this.txtAction.Text = btn.action_id;
        }

        private bool CheckButtonInput()
        {

            if (string.IsNullOrEmpty(this.txtBtnName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：按钮名称不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtAction.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：按钮ID不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (CheckButtonInput())
            {
                CP.Entities.Button_List btn = new CP.Entities.Button_List();
                btn.button_name = this.txtBtnName.Text.Trim();
                btn.action_id = this.txtAction.Text.Trim(); 
                try
                {
                    WebpartController controller = new WebpartController();
                    controller.AddButton(CurrentWebpartID, btn);
                    BindingButton();
                    ExMessageBox.Show("提示信息：新增成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckButtonInput())
            {
                if (string.IsNullOrEmpty(this.txtBtnID.Text.Trim()))
                {
                    ExMessageBox.Show("提示信息：按钮ID不能为空！", AppSetter.SoftName);
                    return;
                }

                CP.Entities.Button_List btn = new CP.Entities.Button_List();
                btn.button_id = Convert.ToInt32(this.txtBtnID.Text);
                btn.button_name = this.txtBtnName.Text.Trim();
                btn.action_id = this.txtAction.Text.Trim();
                try
                {
                    WebpartController controller = new WebpartController();
                    controller.UpdateButton(CurrentWebpartID, btn);
                    BindingButton();
                    ExMessageBox.Show("提示信息：修改成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBtnID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：按钮ID不能为空！", AppSetter.SoftName);
                return;
            }

            try
            {
                WebpartController controller = new WebpartController();
                controller.DeleteButtonByID(CurrentWebpartID, Convert.ToInt32(this.txtBtnID.Text));
                BindingButton();
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void gvList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            CP.Entities.Column_List list = gvList.GetFocusedRow() as CP.Entities.Column_List;
            if (null == list)
                return;
            this.txtListId.Text = list.list_id.ToString();
            this.txtListName.Text = list.list_name;
            this.txtFieldName.Text = list.field_name;
            this.txtGridName.Text = list.grid_name;
        }

        private bool CheckListInput()
        {

            if (string.IsNullOrEmpty(this.txtListName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：列名称不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtFieldName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：列英文名不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtGridName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：Grid Name名不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        private void btnAddList_Click(object sender, EventArgs e)
        {
            if (CheckListInput())
            {
                CP.Entities.Column_List list = new CP.Entities.Column_List();
                list.list_name = this.txtListName.Text.Trim();
                list.field_name = this.txtFieldName.Text.Trim();
                list.grid_name = this.txtGridName.Text.Trim();
                try
                {
                    WebpartController controller = new WebpartController();
                    controller.AddList(CurrentWebpartID, list);
                    BindingList();
                    ExMessageBox.Show("提示信息：新增成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void btnEditList_Click(object sender, EventArgs e)
        {
            if (CheckListInput())
            {
                if (string.IsNullOrEmpty(this.txtListId.Text.Trim()))
                {
                    ExMessageBox.Show("提示信息：列ID不能为空！", AppSetter.SoftName);
                    return;
                }

                CP.Entities.Column_List list = new CP.Entities.Column_List();
                list.list_id = Convert.ToInt32(this.txtListId.Text);
                list.list_name = this.txtListName.Text.Trim();
                list.field_name = this.txtFieldName.Text.Trim();
                list.grid_name = this.txtGridName.Text.Trim();
                try
                {
                    WebpartController controller = new WebpartController();
                    controller.UpdateList(CurrentWebpartID, list);
                    BindingList();
                    ExMessageBox.Show("提示信息：修改成功！", AppSetter.SoftName);
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
            }
        }

        private void btnDelList_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtListId.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：列ID不能为空！", AppSetter.SoftName);
                return;
            }

            try
            {
                WebpartController controller = new WebpartController();
                controller.DeleteListByID(CurrentWebpartID, Convert.ToInt32(this.txtListId.Text));
                BindingList();
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }
    }
}
